<template>
    <div class="box">
        <el-card style="max-width: 100%">
            <template #header>
                <h3>商家注册</h3>
            </template>
            <el-form ref="ruleFormRef" style="max-width: 600px" :model="busList" :rules="rules" class="demo-ruleForm"
                label-width="100px">
                <el-form-item label="商家账号" prop="account">
                    <el-input v-model="busList.account" />
                </el-form-item>
                <el-form-item label="商家密码" prop="password">
                    <el-input v-model="busList.password" />
                </el-form-item>
                <el-form-item label="商家电话" prop="telephone">
                    <el-input v-model="busList.telephone" />
                </el-form-item>
                <el-form-item label="联系人姓名" prop="contact">
                    <el-input v-model="busList.contact" />
                </el-form-item>
                <el-form-item>
                    <el-space :size="57">
                        <el-button type="primary" @click="busRegister()">注册</el-button>
                        <el-button type="warning" round @click="reset()">重置</el-button>
                        <el-link @click="this.$router.push('/busLogin')">已有帐号，点击登录</el-link>
                    </el-space>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import { businessRegisterApi } from "@/api/business.js";
export default {
    data() {
        return {
            busList: [],
            ruleFormRef: "",
            rules: {
                account: { required: true, message: '请输入账号', trigger: 'blur' },
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                telephone: { required: true, message: '请输入手机号', trigger: 'blur' },
                contact: { required: true, message: '请输入联系人', trigger: 'blur' }
            },
        }
    },
    methods: {
        // 点击重置数据
        reset() {
            this.$refs.ruleFormRef.resetFields();
        },
        // 点击注册信息
        busRegister() {
            // 提交数据之前，需要对表单进行统一的校验，只有所有必填项都填写了，才能正常的提交
            this.$refs.ruleFormRef.validate(valid => {
                // valid是一个布尔值，为true表示校验通过，为false表示校验不通过
                if (valid) {
                    businessRegisterApi(this.busList).then(resp => {
                        if (resp.data == 1) {
                            // 响应成功后提示信息，并跳页面
                            ElNotification({
                                title: '注册成功',
                                message: '欢迎您的加入',
                                type: 'success',
                            });
                            // 跳转到登录页面
                            this.$router.push('/busLogin');
                            // 清空注册信息
                            this.busList = {};
                        } else {
                            ElNotification({
                                title: '注册失败',
                                message: '请重新输入',
                                type: 'error',
                            })
                        }
                    })

                }
                return;
            })

        }
    }
}
</script>

<style scoped>
.box {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-image: url(https://www.rrppt.com/uploads/beijing/42583/img/picture.jpg);
    background-size: 100% 100%;
}
.el-card {
    width: 500px;
    opacity: 0.7;
}
</style>